from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Answer import Answer
import time

class Unit3:
    def __init__(self, driver):
        self.Chapter3(driver)

    def Chapter3(self, driver):
        answer = Answer()
        # 点击第三单元的学习
        label = driver.find_element(By.CSS_SELECTOR, '#unit54 > div.book_unit_btn_div > button:nth-child(2)')
        driver.execute_script("arguments[0].click();", label)

        # 开始

        # 读取答案并填入
        file_name = 'Unit3Answer.txt'
        with open(file_name) as file_objECt:
            lines = file_objECt.readlines()

        # c1p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a').click()
        answers = [lines[0]]
        answer.vacancy_question(driver, answers, 1, 1)

        # c2t1n1
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter143"]')))
        driver.find_element(By.XPATH, '//*[@id="chapter143"]').click()
        answers = lines[1].split()
        counts = [1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t1n2
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[2].split()
        counts = [2, 2]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t1n3
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[3]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[3]/a').click()
        answers = lines[3].split()
        counts = [3, 3, 1]
        answer.choice_question(driver, answers, 3, counts, 0)

        # c2t2c1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[4].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t2c2
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[5].split()
        counts = [2, 2, 2, 2]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t3p1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[6], lines[7]]
        answer.vacancy_question(driver, answers, 1, 2)

        # c2t3p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[8].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t3p3
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[3]/div[2]/p[5]/input').send_keys(lines[9])
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[3]/div[2]/p[7]/input').send_keys(lines[10])
        answer.submit(driver, 3)

        # c2t3p4
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(4) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[11].split()
        counts = [2, 2, 2, 2, 2]
        answer.choice_question(driver, answers, 4, counts, 0)

        # c2t3p5
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(5) > a')
        driver.execute_script("arguments[0].click();", label)
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[5]/div[2]/p[5]/input').send_keys(lines[12])
        driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[5]/div[2]/p[7]/input').send_keys(lines[13])
        answer.submit(driver, 5)

        # c2t3p6
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(6) > a')
        driver.execute_script("arguments[0].click();", label)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[6]/div[2]/p[6]/input').click()
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[6]/div[2]/p[9]/input').click()
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[6]/div[2]/p[15]/input').click()
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[6]/div[2]/p[19]/input').click()
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[6]/div[2]/p[26]/input').click()
        answer.submit(driver, 6)

        # c5
        label = driver.find_element(By.CSS_SELECTOR, '#chapter146')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[15], lines[16], lines[17], lines[18], lines[19], lines[20], lines[21], lines[22], lines[23], lines[24]]
        answer.vacancy_question(driver, answers, 1, 10)

        # c6
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter147"]')))
        driver.find_element(By.XPATH, '//*[@id="chapter147"]').click()
        answers = [lines[25], lines[26], lines[27], lines[28], lines[29]]
        answer.vacancy_question(driver, answers, 1, 5)

        # c7t1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter1444')
        driver.execute_script("arguments[0].click();", label)
        answer.submit(driver, 1)

        # c7t2
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[31], lines[32], lines[33], lines[34],lines[35], lines[36], lines[37], lines[38], lines[39], lines[40], lines[41]]
        answer.vacancy_question(driver, answers, 1, 11)

        driver.back()